package com.zzz.offer.convertZ;

public class Solution {
    public static void main(String[] args) {
        System.out.println(new Solution().convert("PAYPALISHIRING", 4));
    }

    public String convert(String s, int numRows) {
        char[][] quenes = new char[numRows][s.length()];

        int cur = 1;

        // 标识哪个位置可以放值
        int[] queneIndex = new int[numRows];
        for(int i = 0; i<numRows; i++) {
            queneIndex[i] = 0;
        }

        int direct = 1; // 1 ++   -1 --
        for(int i=0; i<s.length(); i++) {
            if(cur == 1) {
                direct = 1;
            }

            if(cur == numRows) {
                direct = -1;
            }

            quenes[cur-1][queneIndex[cur-1]++] = s.charAt(i);

            if(numRows > 1) {
                if(direct == 1) {
                    cur++;
                } else {
                    cur--;
                }
            }
        }

        StringBuilder result = new StringBuilder();
        for(int i=0; i<numRows; i++) {
            result.append(new String(quenes[i]), 0, queneIndex[i]);
        }

        return result.toString();
    }
}

